package com.example.bookstore;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
    private List<Items> itemsList;
    private Context mcontext;
    public ItemAdapter(List<Items> itemsList, Context context) {
        this.itemsList = itemsList;
        this.mcontext =context;
    }

    @NonNull
    @Override
    public ItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemAdapter.ViewHolder holder, int position) {
        Items items = itemsList.get(position);
        holder.name.setText(items.getName());
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setmPos(position);
                ((MainActivity)mcontext).changeBookList(position);
                notifyDataSetChanged();
            }
        });
        if(position==getmPos()){
            holder.view.setBackgroundColor(Color.YELLOW);
        }
        else{
            holder.view.setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public int getItemCount() {
        return itemsList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View view;
        TextView name;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            name = (TextView) itemView.findViewById(R.id.item_name);
        }

    }

    public interface GetListener {
        void onClick(int position);
    }

    private GetListener getListener;
    private int mPos;

    public GetListener getGetListener() {
        return getListener;
    }

    public void setGetListener(GetListener getListener) {
        this.getListener = getListener;
    }

    public int getmPos() {
        return mPos;
    }

    public void setmPos(int mPos) {
        this.mPos = mPos;
    }
}